<template>
  <el-dialog
    :title="!dataForm.activityId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="活动名称" prop="activityName">
      <el-input v-model="dataForm.activityName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="充值金额(元)" prop="realMoney">
      <el-input v-model="dataForm.realMoney" type="number" :min="0" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="赠送金额(元)" prop="arriveMoney">
      <el-input v-model="dataForm.arriveMoney" type="number" :min="0"  placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="生效时间" prop="effectTime">
      <el-date-picker v-model="dataForm.effectTime" type="datetime" placeholder="请选择生效时间" default-time="00:00:00"></el-date-picker>
    </el-form-item>
    <el-form-item label="失效时间" prop="expireTime">
      <el-date-picker v-model="dataForm.expireTime" type="datetime" placeholder="请选择失效时间" default-time="23:59:59"></el-date-picker>
    </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="dataForm.status" style="width:100%">
          <el-option v-for="item in statues" :key="item.label" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
    <el-form-item label="活动描述" prop="activityDesc">
      <el-input v-model="dataForm.activityDesc" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          activityId: 0,
          activityName: '',
          realMoney: '0',
          arriveMoney: '0',
          effectTime: '',
          expireTime: '',
          createTime: '',
          activityDesc: '',
          status:'1'
        },
        statues:[{"label":"上架","value":"1"},{"label":"下架","value":"2"} ],
        dataRule: {
          activityName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          realMoney: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          arriveMoney: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          effectTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          expireTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.activityId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.activityId) {
            this.$api.okhttp({
              url: this.$http.adornUrl(`/ecs/ecsrechargeactivity/info/${this.dataForm.activityId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 0) {
             //   console.log(data.ecsrechargeactivity)
                this.dataForm.activityName = data.ecsrechargeactivity.activityName
                this.dataForm.realMoney = data.ecsrechargeactivity.realMoney
                this.dataForm.arriveMoney = data.ecsrechargeactivity.arriveMoney
                this.dataForm.effectTime = new Date(data.ecsrechargeactivity.effectTime)
                this.dataForm.expireTime = new Date(data.ecsrechargeactivity.expireTime)
                this.dataForm.createTime = new Date(data.ecsrechargeactivity.createTime)
                this.dataForm.activityDesc = data.ecsrechargeactivity.activityDesc
                this.dataForm.status = data.ecsrechargeactivity.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if(this.dataForm.effectTime>this.dataForm.expireTime){
              this.$message.error("生效时间不能大于失效时间")
              return ;
            }
            this.$api.okhttp({
              url: this.$http.adornUrl(`/ecs/ecsrechargeactivity/${!this.dataForm.activityId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'activityId': this.dataForm.activityId || undefined,
                'activityName': this.dataForm.activityName,
                'realMoney': this.dataForm.realMoney,
                'arriveMoney': this.dataForm.arriveMoney,
                'effectTime': this.dataForm.effectTime.format('yyyy-MM-dd hh:mm:ss'),
                'expireTime': this.dataForm.expireTime.format('yyyy-MM-dd hh:mm:ss'),
                'activityDesc': this.dataForm.activityDesc,
                'status': this.dataForm.status,
                "customerId": this.$util.cookies.get('customer_id'),
              })
            }).then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
